# coding:utf-8

import time
from lib.base import Base
from page.android import train_timetable_page, train_12306_order_fill_page, train_trips_details_page

__author__ = 'weixin.yuan'


class Train12306OrderFill(Base):
    """12306订单填写"""
    def __init__(self, method_name):
        Base.__init__(self, method_name)

        """
        本页元素
        """
        self.yuding_xz_btn = train_12306_order_fill_page.elements['预订须知']
        self.shikebiao_btn = train_12306_order_fill_page.elements['时刻表']
        self.jieshou_wz_btn = train_12306_order_fill_page.elements['接受无座']
        self.fuwuxieyi_btn = train_12306_order_fill_page.elements['火车票信息服务协议']
        self.tj_chengke_btn = train_12306_order_fill_page.elements['添加乘客']
        self.tjbj_chengke_btn = train_12306_order_fill_page.elements['添加/编辑乘客']
        self.tj_ertong_btn = train_12306_order_fill_page.elements['添加儿童']
        self.xz_chengke_btn1 = train_12306_order_fill_page.elements['选择乘客']
        self.wancheng_btn1 = train_12306_order_fill_page.elements['完成']
        self.wancheng_btn2 = train_12306_order_fill_page.elements['确定添加乘客']
        self.lxr_btn = train_12306_order_fill_page.elements['联系人']
        self.mobile_btn = train_12306_order_fill_page.elements['手机号']
        self.mobile_input = train_12306_order_fill_page.elements['通知出票信息']
        self.commit_btn = train_12306_order_fill_page.elements['提交订单']
        self.go_on_btn = train_12306_order_fill_page.elements['继续预订']
        self.not_buy_btn = train_12306_order_fill_page.elements['暂不购买，跳过']
        self.yzf_title1 = train_12306_order_fill_page.elements['已支付title1']
        self.yzf_title2 = train_12306_order_fill_page.elements['已支付title2']
        self.close_btn = train_12306_order_fill_page.elements['火车票信息服务协议_title']
        self.fuwuxieyi_title = train_12306_order_fill_page.elements['火车票信息服务协议_title']
        """
        外部页面元素
        """
        self.yuding_btn = train_trips_details_page.elements['预定']
        self.yuding_12306_btn = train_trips_details_page.elements['12306预订']
        self.shikebiao_title = train_timetable_page.elements['时刻表']

    def test_12306_order_fill(self):
        """12306订单填写"""
        self.open_schema_android('6013')

        self.click(self.yuding_btn)
        self.click(self.yuding_12306_btn)

        if self.is_exits(self.yuding_xz_btn, times=5):
            self.click(self.yuding_xz_btn)
            self.click(self.close_btn)

        self.click(self.shikebiao_btn)
        self.check(self.shikebiao_title)
        self.back()

        if self.is_exits(self.fuwuxieyi_btn, times=5):
            self.click(self.fuwuxieyi_btn)
            self.check(self.fuwuxieyi_title)
            self.back()

        if self.is_exits(self.tj_chengke_btn, times=5):
            self.click(self.tj_chengke_btn)
            time.sleep(5)
            self.click(self.xz_chengke_btn1)
            self.click(self.wancheng_btn2)
        else:
            self.click(self.tjbj_chengke_btn)
            self.click(self.wancheng_btn2)

        if self.is_exits(self.mobile_input):
            self.click(self.lxr_btn)
            self.click(self.mobile_btn)

        self.click(self.commit_btn)

        self.click(self.go_on_btn, is_necessary=False, times=10)
        self.click(self.not_buy_btn, is_necessary=False, times=5)
        if not self.is_exits(self.yzf_title1, times=60) and not self.is_exits(self.yzf_title2, times=60):
            raise AssertionError('没有找到已支付和支付收银台title')
